var util = require('../../utils/util.js')
var app = getApp()
let videoAd = null
let zhi = null
let openid  = null
let jili1= 'adunit-c3a014146b7c9efa'
Page({
  data: {
    lookVideosNum: 10,
    isDisabled: false,
    yuanshengAd1:'adunit-df84d2891075f223',
  },
  // 异步获取用户openid
  async getOpenIdsync() {
    return await wx.cloud.callFunction({
      name: 'user',
      data: {
        tag: 'getOpenId'
      }
    }).then(res => {
      return res.result
    })
  },
  // 异步获取系统控制变量
  getController() {
    
    if (app.globalData.SysController == null) {
      app.getController().then(res => {
         zhi = res.jinbi_zhi.look_ads
         jili1 = res.ad.jili1
         this.setData({
          yuanshengAd1:res.ad.yuansheng1
         })
      })
    } else {
      this.setData({
        yuanshengAd1:app.globalData.SysController.ad.yuansheng1
       })
      zhi = app.globalData.SysController.jinbi_zhi.look_ads
      jili1=app.globalData.SysController.ad.jili1
    }
  },
  onShow: function () {
    this.getLookVideos_qiandao() //  获取观看和签到剩余次数及时间
   this.createRewardedVideoAd() 

  },
  onLoad: function (options) {
    this.getController()//异步获取系统控制变量
  },
  // 定义查询方法
  dbLookVideosNum(_openid) {
    wx.cloud.database().collection('lookvideos_qiandao').where({
        _openid
      }).get()
      .then(res => {
        this.setData({
          lookVideosNum: res.data[0].lookVideosNum
        })
      })
  },
  //  获取观看和签到剩余次数及时间
  getLookVideos_qiandao() {
    if (!app.globalData.userInfo) {
      this.getOpenIdsync().then(res => {
        this.dbLookVideosNum(res)
        openid = res
      })
    } else {
      this.dbLookVideosNum(app.globalData.userInfo._openid)
      openid = app.globalData.userInfo._openid
    }
  },
  // 观看
  look(e) {
    // 用户触发广告后，显示激励视频广告
    if (videoAd) {
      videoAd.show().catch(() => {
        // 失败重试
        videoAd.load()
          .then(() => videoAd.show())
          .catch(err => {
            wx.showToast({
              icon: 'none',
              title: '广告未就绪，稍后再来',
            })
          })
      })
    } else {
      this.setData({
        isDisabled: true
      })
      wx.showToast({
        icon: 'none',
        title: '广告未就绪，稍后再来',
      })
      setTimeout(() => {
        this.setData({
          isDisabled: false
        })
      }, 3000)
    }
  },


  // 跳转其他小程序
  yesSend() {
    wx.navigateToMiniProgram({
      appId: 'wx3814d38ca5211136',
      path: `/pages/news/news`,
      success(res) {
        // 打开成功
      }
    })
  },

  // 在页面onLoad回调事件中创建激励视频广告实例
  createRewardedVideoAd() {
    if (wx.createRewardedVideoAd) {
      videoAd = wx.createRewardedVideoAd({
        adUnitId: jili1
      })
      videoAd.onLoad(() => {
      })
      videoAd.onError(() => {
      })
      videoAd.onClose((res) => {
        if (res.isEnded) {
          this.addJinbi()
          this.subLookNum() //  获取观看和剩余次数及时间
          wx.showToast({
            icon: 'none',
            title: `积分+${zhi}`,
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: '没有奖励',
          })
        }
      })
    }
  },
  // 添加积分
  addJinbi() {
    wx.cloud.database().collection('user').where({
      _openid:openid
    })
    .update({
      data: {
        jinbiNum: wx.cloud.database().command.inc(zhi)
      }
    })
  },
  // 观看次数减1
  subLookNum() {
    wx.cloud.database().collection('lookvideos_qiandao').where({
      _openid:openid
    })
    .update({
      data: {
        lookVideosNum: wx.cloud.database().command.inc(-1)
      }
    }).then(() => {
      this.setData({
        lookVideosNum:this.data.lookVideosNum-1
      })
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
})